package com.pet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet.dto.PetAddDTO;
import com.pet.dto.PetQueryDTO;
import com.pet.dto.PetUpdateDTO;
import com.pet.dto.Result;
import com.pet.entity.HealthRecordDO;
import com.pet.entity.PetDO;
import com.pet.entity.UserDO;
import com.pet.mapper.HealthRecordMapper;
import com.pet.mapper.PetMapper;
import com.pet.service.PetService;
import com.pet.utils.UserContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
public class PetServiceImpl extends ServiceImpl<PetMapper, PetDO> implements PetService {

    @Resource
    private PetMapper petMapper;
    @Resource
    private HealthRecordMapper healthRecordMapper;

    @Override
    public Result add(PetAddDTO dto) {
        if (StringUtils.isBlank(dto.getName())) {
            return Result.error("宠物名称不能为空");
        }
        PetDO petDO = new PetDO();
        BeanUtil.copyProperties(dto, petDO);
        UserDO currentUser = UserContextUtil.getCurrentUser();
        petDO.setUserId(currentUser.getId());
        petMapper.insert(petDO);
        return Result.success("添加成功");
    }

    @Override
    public List<PetDO> queryList(PetQueryDTO dto) {
        UserDO currentUser = UserContextUtil.getCurrentUser();
        LambdaQueryWrapper<PetDO> queryWrapper = Wrappers.<PetDO>lambdaQuery()
                .eq(PetDO::getUserId, currentUser.getId())
                .like(StringUtils.isNotBlank(dto.getName()), PetDO::getName, dto.getName())
                .eq(StringUtils.isNotBlank(dto.getBreed()), PetDO::getBreed, dto.getBreed())
                .eq(StringUtils.isNotBlank(dto.getGender()), PetDO::getGender, dto.getGender())
                .eq(Objects.nonNull(dto.getSterilized()), PetDO::getSterilized, dto.getSterilized())
                .eq(Objects.nonNull(dto.getVaccinated()), PetDO::getVaccinated, dto.getVaccinated())
                .orderByDesc(PetDO::getCreatedAt);
        return petMapper.selectList(queryWrapper);
    }

    @Override
    public Result updatePet(PetUpdateDTO dto) {
        LambdaUpdateWrapper<PetDO> updateWrapper = Wrappers.<PetDO>lambdaUpdate()
                .eq(PetDO::getId, dto.getId())
                .set(StringUtils.isNotBlank(dto.getName()), PetDO::getName, dto.getName())
                .set(PetDO::getBreed, dto.getBreed())
                .set(PetDO::getBirthDate, dto.getBirthDate())
                .set(PetDO::getGender, StringUtils.defaultIfBlank(dto.getGender(), "未知"))
                .set(PetDO::getAdoptionDate, dto.getAdoptionDate())
                .set(PetDO::getSterilized, Objects.isNull(dto.getSterilized()) ? 1 : dto.getSterilized())
                .set(PetDO::getVaccinated, Objects.isNull(dto.getVaccinated()) ? 1 : dto.getVaccinated())
                .set(Objects.nonNull(dto.getAvatar()), PetDO::getAvatar, dto.getAvatar())
                .set(PetDO::getWeight, dto.getWeight());
        this.update(updateWrapper);
        return Result.success();
    }

    @Override
    public Result deletePet(PetUpdateDTO dto) {
        LambdaUpdateWrapper<PetDO> updateWrapper = Wrappers.<PetDO>lambdaUpdate()
                .eq(PetDO::getId, dto.getId());
        this.remove(updateWrapper);
        // 删除宠物健康记录
        LambdaUpdateWrapper<HealthRecordDO> recordUpdateWrapper = Wrappers.<HealthRecordDO>lambdaUpdate()
                .eq(HealthRecordDO::getPetId, dto.getId());
        healthRecordMapper.delete(recordUpdateWrapper);
        return Result.success();
    }
}